package edu.csumb.itcd.teammario;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;


public class RollEnemy extends Enemy {

	private boolean targetFound;
	private boolean pathFound;
	private Ellipse2D lineOfSight;
	private int circumference = 300;
    private int newSpeed;
	
	public RollEnemy(Point spawnPoint) {
		super(spawnPoint);
		
		targetFound = false;
		pathFound = false;
		lineOfSight = new Ellipse2D.Double(enemyPosition.getX() - (circumference/2), enemyPosition.getY() - (circumference/2), circumference, circumference);
		// TODO Auto-generated constructor stub
	}

	public void moveAi(int playerX, int playerY)
	{
		if (targetFound && visible)
		{
			if (pathFound)
			{
				roll();
			} else
			{
				findPath();
			}
		} else
		{
			if (lineOfSight.intersects(Player.getPlayerImage()))
			{
				targetFound = true;
			}
			
//			if ((enemyPosition.x + width / 2) - 5 < playerX)
//            {
//                enemyPosition.x = enemyPosition.x + speed ;
//            }
//            else if ((enemyPosition.x + width / 2) - 5 > playerX)
//            {
//                enemyPosition.x = enemyPosition.x - speed;
//            }
//            if ((enemyPosition.y + height /2) - 5 > playerY)
//            {
//                enemyPosition.y = enemyPosition.y - speed;
//            }
//            else if((enemyPosition.y + height /2) - 5 < playerY)
//            {
//                enemyPosition.y = enemyPosition.y + speed;
//            }
			vectorY = playerY - (height/2) + (Player.getHeight()/2) - enemyPosition.getY();
	        vectorX = playerX - (width/2) + (Player.getWidth()/2) - enemyPosition.getX();

	        //The dist variables need to be positive for the trig equations.
	        distY = Math.abs(vectorY);
	        distX = Math.abs(vectorX);

	        //Gives the vectors a value of 1 or -1.
	        vectorY /= distY;
	        vectorX /= distX;

	        /**
	         * tan(x) = opposite/adjacent
	         * tan(x) = distY/distX
	         * x = arctan(distY/distX)
	         *
	         * cos(x) = adjacent/hypotenuse
	         * cos(x) = moveX/speed
	         * moveX = speed * cos(x)
	         * moveX = speed * cos(arctan(distY/distX))
	         */
	        if (distX == 0 && distY == 0)
	        {
	            moveY = 0;
	            moveX = 0;
	        } else if (distX == 0)
	        {
	        	moveY = speed * vectorY;
	        	moveX = 0;
	        } else if (distY == 0)
	        {
	        	moveY = 0;
	        	moveX = speed * vectorX;
	        } else
	        {
	            moveY = speed * Math.sin(Math.atan(distY/distX));
	            moveX = speed * Math.cos(Math.atan(distY/distX));
//	            moveY = Math.rint(moveY) * vectorY;
//	            moveX = Math.rint(moveX) * vectorX;
	            moveY = moveY * vectorY;
	            moveX = moveX * vectorX;
	        }
	        
	        enemyPosition.setLocation(enemyPosition.getX() + moveX, enemyPosition.getY() + moveY);
		}
		
		lineOfSight = new Ellipse2D.Double(enemyPosition.getX() - (circumference/2), enemyPosition.getY() - (circumference/2), circumference, circumference);
		
		if (currentHealth <= 0)
        {
            isAlive = false;
            LeaderBoard.incrementKillCount();
        }
		
		if (enemyPosition.getX() > 0 && enemyPosition.getX() + width < 800 - 5 && enemyPosition.getY() > 55 && enemyPosition.getY() + height < 600 - 30)
        {
        	visible = true;
        }
	}
	
	private void roll()
	{		
		speed = newSpeed;
		
		if (moveX > 0)
		{
			for (int i = 0; i <= moveX; i++)
			{
				enemyPosition.setLocation(enemyPosition.getX() + vectorX, enemyPosition.getY());
				
				if (enemyPosition.getX() <= 0 || enemyPosition.getX() >= 800 - width - 5)
				{
					vectorX *= -1;
					newSpeed -= 3;
				}
			}
		} else
		{
			for (int i = 0; i >= moveX; i--)
			{
				enemyPosition.setLocation(enemyPosition.getX() + vectorX, enemyPosition.getY());
				
				if (enemyPosition.getX() <= 0 || enemyPosition.getX() >= 800 - width - 5)
				{
					vectorX *= -1;
					newSpeed -= 3;
				}
			}
		}
		
		if (moveY > 0)
		{
			for (int i = 0; i <= moveY; i++)
			{
				enemyPosition.setLocation(enemyPosition.getX(), enemyPosition.getY() + vectorY);
					
				if (enemyPosition.getY() <= 55 || enemyPosition.getY() >= 600 - 30 - height)
				{
					vectorY *= -1;
					newSpeed -= 3;
				}
			}
		} else
		{
			for (int i = 0; i >= moveY; i--)
			{
				enemyPosition.setLocation(enemyPosition.getX(), enemyPosition.getY() + vectorY);
				
				if (enemyPosition.getY() <= 55 || enemyPosition.getY() >= 600 - 30 - height)
				{
					vectorY *= -1;
					newSpeed -= 3;
				}
			}
		}
		
		moveY = moveY/speed * newSpeed;
        moveX = moveX/speed * newSpeed;
		
		if (newSpeed <= 3)
		{
			speed = 1;
			targetFound = false;
			pathFound = false;
		}
	}
	
	private void findPath()
	{
		speed = 16;
		newSpeed = speed;
		
		vectorY = Player.getPlayerPosition().y - (Player.getWidth()/2) - enemyPosition.getY();
        vectorX = Player.getPlayerPosition().x - (Player.getWidth()/2) - enemyPosition.getX();

        distY = Math.abs(vectorY);
        distX = Math.abs(vectorX);

        vectorY /= distY;
        vectorX /= distX;

        if (distX == 0 && distY == 0)
        {
            moveY = 0;
            moveX = 0;
        } else if (distX == 0)
        {
        	moveY = speed * vectorY;
        	moveX = 0;
        } else if (distY == 0)
        {
        	moveY = 0;
        	moveX = speed * vectorX;
        } else
        {
            moveY = speed * Math.sin(Math.atan(distY/distX));
            moveX = speed * Math.cos(Math.atan(distY/distX));
//            moveY = Math.rint(moveY) * vectorY;
//            moveX = Math.rint(moveX) * vectorX;
            moveY = moveY * vectorY;
            moveX = moveX * vectorX;
        }
        
        pathFound = true;
	}
	
	public void paintEnemy(Graphics2D g2)
    {
    	if (isAlive == true)
        {
            //temp is to make sure the number is in bounds for the new Color
            int temp = (currentHealth*165)/40;
            if (temp < 0)
                temp = 0;
            else if (temp > 255)
                temp = 255;
            
            enemyc = new Color(225,temp,255);

            g2.setColor(enemyc);
            if (poisonReceived.isCurrentlyPoisoned() == true)
            {
            	g2.setColor(Color.GREEN);
            }
            else
            {
            	g2.setColor(enemyc);
            }
            g2.fill(new Ellipse2D.Double(enemyPosition.getX(), enemyPosition.getY(), width, height));
            g2.setColor(Color.black);
            g2.draw(new Ellipse2D.Double(enemyPosition.getX(), enemyPosition.getY(), width, height));
        }
    }
}
